package org.maven.dependencybrowser.model;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.controller.model.ArtifactVersionsListManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class describing duplicate artifacts in a dependency tree
 */
public class Duplicate
{
    private String identifier;
    private List<ArtifactInstanceMap> versions;
    private List<Duplicate> duplicatesList;

    private boolean selected;

    public Duplicate(ArtifactInstanceMap map, List<Duplicate> duplicates)
    {
        this.identifier = map.getGroupId() + ":" + map.getArtifactId();
        this.duplicatesList = duplicates;
        versions = new ArrayList<ArtifactInstanceMap>();
        versions.add(map);
    }

    public void addVersion(ArtifactInstanceMap map)
    {
        if (versions.contains(map))
        {
            return;
        }
        versions.add(map);
        if (versions.size() == 2)
        {
            duplicatesList.add(this);
        }
    }

    public String getIdentifier()
    {
        return identifier;
    }

    public void select(boolean state, ArtifactVersionsListManager artifactVersionsListManager)
    {
        selected = state;
        artifactVersionsListManager.select(versions);
    }


    public boolean isSelected()
    {
        return selected;
    }

    public String getVersions()
    {
        StringBuffer versionList = new StringBuffer();
        for (Iterator iter = versions.iterator(); iter.hasNext();)
        {
            ArtifactInstanceMap version = (ArtifactInstanceMap) iter.next();
            versionList.append(version.getVersion());
            if (iter.hasNext())
            {
                versionList.append(", ");
            }
        }
        return versionList.toString();
    }

}
